package com.sxpi.zixun.controller.adminuser;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxpi.zixun.aspect.auth.HasAuth;
import com.sxpi.zixun.bo.AdminUserBo;
import com.sxpi.zixun.common.enums.AdminUserResultEnum;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.controller.BaseController;
import com.sxpi.zixun.service.adminuser.IAdminUserService;
import com.sxpi.zixun.vo.AdminUserRegVo;
import com.sxpi.zixun.vo.AdminUserUpdateVo;
import com.sxpi.zixun.vo.AdminUserVo;
import lombok.extern.slf4j.Slf4j;
import org.pug.generator.anno.PugDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 董伟豪
 * @Date: 2022/8/24 21:27
 */
@RestController
@Slf4j
@PugDoc(name="后台用户管理",tabname = "kss_admin_user")
public class AdminUserController extends BaseController {

    @Autowired
    private IAdminUserService adminUserService;

    /**
     * 保存和修改后台用户管理
     * @param adminUserRegVo
     * @return
     */
    @PostMapping("/adminuser/saveupdate")
    @PugDoc(name="保存和修改后台用户管理")
    @HasAuth("130107")
    public AdminUserBo saveUpdateAdminUser(@RequestBody @Validated AdminUserRegVo adminUserRegVo) {
        return adminUserService.saveUpdateAdminUser(adminUserRegVo);
    }

    /**
     * 查询后台用户管理列表信息并分页
     * @param adminUserVo
     * @return
     */
    @PostMapping("/adminuser/load")
    @PugDoc(name = "查询后台用户管理列表信息并分页")
    @HasAuth("130105")
    public IPage<AdminUserBo> findAdminUsers(@RequestBody AdminUserVo adminUserVo){
        return adminUserService.findAdminUserPage(adminUserVo);
    }

    /**
     * 根据后台用户管理id删除后台用户管理
     * @param id
     * @return
     */
    @PostMapping("/adminuser/delete/{id}")
    @PugDoc(name = "根据后台用户管理id删除后台用户管理")
    @HasAuth("130108")
    public int deleteAdminUserById(@PathVariable("id") String id){
        Vsserts.isEmptyEx(id, AdminUserResultEnum.ID_NOT_EMPTY);
        return adminUserService.deleteAdminUserById(new Long(id));
    }

    /**
     * 根据后台用户管理ids批量删除后台用户管理
     * @param adminUserVo
     * @return
     */
    @PostMapping("/adminuser/deleteBatch")
    @PugDoc(name = "根据后台用户管理ids批量删除后台用户管理")
    @HasAuth("130109")
    public Boolean deleteAdminUser(@RequestBody AdminUserVo adminUserVo){
        log.info("你要批量删除的IDS是：{}",adminUserVo.getBatchIds());
        Vsserts.isEmptyEx(adminUserVo.getBatchIds(), ResultStatusEnum.ID_NOT_EMPTY);
        return adminUserService.deleteBatchAdminUser(adminUserVo.getBatchIds());
    }

    /**
     * 用户状态更新
     * @param adminUserUpdateVo
     * @return
     */
    @PostMapping("/adminuser/update")
    @PugDoc(name = "用户状态更新")
    @HasAuth("130110")
    public Boolean updateAdminUser(@RequestBody AdminUserUpdateVo adminUserUpdateVo){
        return adminUserService.updateAdminUser(adminUserUpdateVo);
    }

    /**
     * 给用户绑定角色
     * @param userId
     * @param roleId
     * @return
     */
    @PostMapping("/adminuser/binding/role")
    @PugDoc(name = "给用户绑定角色")
    public int bindingRole(@RequestParam("userId") Long userId,@RequestParam("roleId") Long roleId) {
        return adminUserService.saveUserRole(userId,roleId);
    }

    /**
     * 给用户解绑角色
     * @param userId
     * @param roleId
     * @return
     */
    @PostMapping("/adminuser/unbing/role")
    @PugDoc(name = "给用户解绑角色")
    public int unbindingRole(Long userId, Long roleId) {
        return adminUserService.deleteUserRole(userId,roleId);
    }
}
